package com.example.listviewtest;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.transition.Hold;

import java.util.List;

public class AnimalAdapter extends ArrayAdapter<Animal> {

    private int resourceId;
    private Context context;

    public AnimalAdapter(@NonNull Context context, int resource, @NonNull List<Animal> objects) {
        super(context, resource, objects);
        this.resourceId = resource;
        this.context = context;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Animal item = getItem(position);
        View view;
        ViewHolder holder;
        if (convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            holder = new ViewHolder();
            holder.name = view.findViewById(R.id.tvName);
            holder.englishName = view.findViewById(R.id.tvEnglishName);
            view.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
            view = convertView;
        }
        holder.name.setText(item.getName());
        holder.englishName.setText(item.getEnglishName());
        return view;
    }

    class ViewHolder {
         TextView name;
         TextView englishName;
    }
}
